The function prototype for wait () is: #include #include pid_t wait (int *status)
Once the process has called wait, it blocks itself immediately, and the wait automatically analyzes whether a subprocess of the current process has exited. If you
Wifexited/wexitstatus/wifsignaled if the exit status value (* Note program termination: :) of the childprocess is zero, then the status value reported by 'waitpid 'or 'wait' is also zero. you can test for other kinds of information encoded inthe
System Functions
Function: Call fork to generate a sub-process, which is called by the sub-process;/bin/sh-C command to execute the command represented by the command parameter, blocking the current process until the command is killed
The execution
INT system (const char * character string){Pid_t PID;Int status;
If (else string = NULL ){Return (1 );}
If (pid = fork () Status =-1;}Else if (pid = 0 ){Execl ("/bin/sh", "sh", "-c", character string, (char *) 0 );-Exit (127); // The sub-process
Cases:[CPP]View PlainCopy
Status = System ("./test.sh");
1, first unified two statements: (1) System return value: Refers to the return value after invoking the system function, such as the status system return value (2) Shell return
Before studying the wait and WAITPID functions, I was puzzled by the use of macro wifexited to check the resulting process termination state: Typically we exit by calling the exit or _exit function in the program. So the terminating state of the
Example:
Status = system ("./test. Sh ");
1. Two statements are unified: (1) system return value: the return value after the system function is called. For example, in the previous example, status is system return value (2) Shell return value: the
System calls return values to determine whether the command is successfully executed. system calls
The system function processes the returned values in three stages:Phase 1: Create Sub-processes and other preparations. If it fails,-1 is returned.
Wait (), waitpid () functionBrief introduction1) When a process is normal or abnormally terminated, the kernel sends a SIGCHLD signal to its parent process. The parent process can ignore the signal, or provide a handler for the signal. By default,
If the child process ends before the parent process, the parent process calls the wait () function and does not call the wait () function to produce two different results: -- if the parent process does not call wait () and waitpid () function, the
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.